package com.joplus.housekeeper.utils;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Environment;
import android.provider.Settings;
import android.text.ClipboardManager;
import android.text.TextUtils;

import java.io.File;
import java.util.UUID;

//跟App相关的辅助类
public class AppUtils {

    private AppUtils() {
        /* cannot be instantiated */
        throw new UnsupportedOperationException("cannot be instantiated");

    }

    /**
     * 获取应用程序名称
     */
    public static String getAppName(Context context) {
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    context.getPackageName(), 0);
            int labelRes = packageInfo.applicationInfo.labelRes;
            return context.getResources().getString(labelRes);
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * [获取应用程序版本名称信息]
     *
     * @param context
     * @return 当前应用的版本名称
     */
    public static int getVersionName(Context context) {
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    context.getPackageName(), 0);
            return packageInfo.versionCode;

        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        return 0;
    }


    /**
     * 应用分享
     *
     * @param context   上下文
     * @param shareInfo 分享的内容
     */
    public static void share(Context context, String shareInfo) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/*");
        intent.putExtra(Intent.EXTRA_TEXT, shareInfo);
        context.startActivity(intent);
    }

    /**
     * 应用商店评价
     *
     * @param context 上下文
     */
    public static void assess(Context context) {
        Uri uri = Uri.parse("market://details?id=" + getAppName(context));
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    /**
     * 判断应用程序是否安装
     *
     * @param context
     * @param packageName 应用包名
     * @return true 已安装
     */
    public static boolean isAppInstalled(Context context, String packageName) {
        PackageInfo packageInfo;
        try {
            packageInfo = context.getPackageManager().getPackageInfo(
                    packageName, 0);
        } catch (NameNotFoundException e) {

            e.printStackTrace();
            packageInfo = null;
        }

        if (null == packageInfo) {
            return false;
        } else {
            return true;//已安装
        }

    }


    /**
     * 获取设备ID
     *
     * @param context
     * @return
     */
    public static String getDeviceId(Context context) {

        String deviceid = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        if (StringUtils.isEmpty(deviceid)) {
            return deviceid;
        }

        return UUID.randomUUID().toString();
    }

    /**
     * 复制
     *
     * @param context
     * @param copyStr
     * @return
     */
    public static boolean clipboard(Context context, String copyStr) {

        if (TextUtils.isEmpty(copyStr)) {
            return false;
        } else {
            ClipboardManager clip = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
            clip.getText(); // 粘贴
            clip.setText(copyStr); // 复制
            return true;
        }
    }

    /**
     * 安装
     *
     * @param context 接收外部传进来的context
     */
    public static void install(Context context, String apkPath) {
        // 核心是下面几句代码
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(new File(apkPath)),
                "application/vnd.android.package-archive");
        context.startActivity(intent);
    }


    /**
     * 是否安装微信
     *
     * @param con
     * @return
     */
    public static Boolean isWeChatInstanlled(Context con) {
        return isAppInstalled(con, "com.tencent.mm");
    }

    /**
     * 判断是否在前台
     *
     * @param context
     * @return
     */
    public static boolean isRunningForeground(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
        String currentPackageName = cn.getPackageName();
        if (!TextUtils.isEmpty(currentPackageName) && currentPackageName.equals(context.getPackageName())) {
            return true;
        }
        return false;
    }


    public static void sendUpdateMessage(Context mContext, String version) {
        File file = new File(Environment.getExternalStorageDirectory() + "/Download/joplus_" + version + ".apk");
        //通知应用更新
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(android.content.Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(file),
                "application/vnd.android.package-archive");
        mContext.startActivity(intent);
    }
}
